﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ArgusLight.Fractals2D;

namespace Fractals
{
	public class ControlFractalParameter
	{
		protected Grid grid;
		protected NumericUpDown nudWidth;
		protected NumericUpDown nudHeight;
		protected NumericUpDown nudRotation;

		public ControlFractalParameter()
		{
		}

		public virtual UIElement GetUIElement()
		{
			if (this.grid != null)
				return this.grid;

			this.grid = new Grid();

			this.AddLabel(Localization.Text.FractalParameterPixelWidth, 0, 0);
			this.AddLabel(Localization.Text.FractalParameterPixelHeight, 1, 0);
			this.AddLabel(Localization.Text.FractalParameterRotation, 2, 0);

			this.nudWidth = this.getNud();
			this.nudWidth.Minimum = 100;
			this.nudWidth.Maximum = 2000;
			this.AddToGrid(this.nudWidth, 0, 1);

			this.nudHeight = this.getNud();
			this.nudHeight.Minimum = 100;
			this.nudHeight.Maximum = 2000;
			this.AddToGrid(this.nudHeight, 1, 1);

			this.nudRotation = this.getNud();
			this.nudRotation.Minimum = 0;
			this.nudRotation.Maximum = 360;
			this.AddToGrid(this.nudRotation, 2, 1);
			return this.grid;
		}

		protected void AddLabel(string text, int row, int col)
		{
			TextBlock tb = new TextBlock();
			tb.Foreground = new SolidColorBrush(Colors.White);
			tb.VerticalAlignment = System.Windows.VerticalAlignment.Center;
			tb.Margin = new Thickness(5, 5, 0, 0);
			tb.Text = text;
			this.AddToGrid(tb, row, col);
		}

		protected NumericUpDown getNud()
		{
			NumericUpDown nud = new NumericUpDown();
			nud.Foreground = new SolidColorBrush(Colors.White);
			nud.BorderBrush = new SolidColorBrush(Colors.White);
			nud.Margin = new Thickness(5, 5, 5, 0);
			nud.VerticalAlignment = System.Windows.VerticalAlignment.Center;
			nud.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			return nud;
		}

		protected void AddToGrid(FrameworkElement c, int row, int col)
		{
			while (this.grid.RowDefinitions.Count <= row)
			{
				RowDefinition rd = new RowDefinition();
				rd.Height = GridLength.Auto;
				this.grid.RowDefinitions.Add(rd);
			}
			while (this.grid.ColumnDefinitions.Count <= col)
			{
				ColumnDefinition cd = new ColumnDefinition();
				cd.Width = GridLength.Auto;
				this.grid.ColumnDefinitions.Add(cd);
			}

			Grid.SetRow(c, row);
			Grid.SetColumn(c, col);
			this.grid.Children.Add(c);
		}

		public virtual void SetValues(FractalParameter fParameter)
		{
			this.nudWidth.Value = fParameter.PixelWidth;
			this.nudHeight.Value = fParameter.PixelHeight;
			this.nudRotation.Value = fParameter.Rotation * 180 / Math.PI;
		}

		public virtual void GetValues(ref FractalParameter fParameter)
		{
			fParameter.PixelWidth = (int)this.nudWidth.Value;
			fParameter.PixelHeight = (int)this.nudHeight.Value;
			fParameter.Rotation = this.nudRotation.Value * Math.PI / 180;
		}
	}
}
